class ReceiptsController < ApplicationController
  include ControllerHelper
  before_filter :init, :only => [:index, :show]
  before_filter :require_moderator, :except => [:index, :show]
  def new
    set_form_settings
    @page["name"] = "receipt"
    @receipt = Receipt.new
    render :layout => "form"
  end

  def create
    begin
      @receipt = Receipt.new(params[:receipt])
      if @receipt.save
        redirect_to receipt_moderator_path(:status => "all") and return
      else
        set_form_settings
        @page["name"] = "receipt"
        render :action => "new", :layout => "form"
      end
    rescue
      flash[:notice] = I18n.t "site.message.corrupted_html"
      set_form_settings
      @page["name"] = "receipt"
      render :action => "new", :layout => "form"
    end
  end

  def moderator
    per_page = params[:per_page] ||= @site["per_page"]["moderator"]["receipts"]
    conditions = []
    conditions = merge_conditions(conditions, ["status = ?", "approved"]) if params[:status] == "approved"
    conditions = merge_conditions(conditions, ["status <> ?", "approved"]) if params[:status] == "banned"
    @receipts = Receipt.find(:all, :conditions => conditions).uniq.paginate(:page => params[:page], :per_page => per_page)
    set_moderator_settings
    render :layout => "cms"
  end

  def destroy
    @receipts = Receipt.find(:first, :conditions => ["id = ?", params[:id]])
    if @receipts
      @receipts.destroy
    else
      flash[:notice] = I18n.t "site.message.no_receipt"
    end
    redirect_to receipt_moderator_path(:status => "all")
  end

  def edit
    set_form_settings
    @page["name"] = "receipt"
    @receipt = Receipt.find(:first, :conditions => ["id = ?", params[:id]])
    if @receipt.nil?
      flash[:notice] = I18n.t "site.message.no_receipt"
      redirect_to receipt_moderator_path(:status => "all")
    else
      render :layout => "form"
    end
  end

  def update
    @receipt = Receipt.find(:first, :conditions => ["id = ?", params[:id]])
    if @receipt.nil?
      flash[:notice] = I18n.t "site.message.no_receipt"
      redirect_to receipt_moderator_path(:status => "all")
    else
      if @receipt.update_attributes(params[:receipt])
        redirect_to receipt_moderator_path(:status => "all") and return
      else
        set_form_settings
        @page["name"] = "receipt"
        render :layout => "form", :action => "edit", :id => params[:id]
      end
    end
  end

  def approve_ban
    receipt = Receipt.find(params[:id])
    old_status = receipt.status == "approved" ? "approved" : "banned"
    receipt.status = receipt.status == "approved" ? "banned" : "approved"
    receipt.save
    redirect_to receipt_moderator_path(:status => old_status)
  end

  def index
    per_page = params[:per_page] ||= @site["per_page"]["entity"]["receipts"]
    @receipts = Receipt.find(:all, :conditions => ["status = ?", "approved"]).uniq.paginate(:page => params[:page], :per_page => per_page)
    @page["bonus_title"] = @page["bonus_description"] = @receipts.map do |item| item.title end.to_sentence
    @page["bonus_keywords"] = @receipts.map do |item| item.title.gsub(" ", ",") end.join(",")
  end

  def show
    @receipt = Receipt.find(:first, :conditions => ["id = ?", params[:id]])
    if @receipt.nil?
      flash[:notice] = I18n.t "site.message.no_receipt"
      redirect_to receipts_path and return
    end
    @page["bonus_title"] = @page["bonus_description"] = @receipt.title
    @page["bonus_keywords"] = @receipt.title.gsub(" ", ",")
  end

end
